package com.intercom.fdfr;

import android.content.Context;
import android.graphics.Canvas;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;


public class FaceRecoView extends RelativeLayout {

    private static final String TAG = "FaceRecoView";
    
    private Context context;

    private CameraPreview previewSurface;
    public CameraController cameraController;
    private CameraDetectView cameraDetectView;
    private int numberOfCameras;
    private boolean useCamera0 = true;

    private Handler mainHandler;

    public FaceRecoView(Context context) {
        super(context);
        Log.i(TAG, "FaceRecoView: (Context context)");
        initView(context);
    }

    public FaceRecoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.i(TAG, "FaceRecoView: (Context context, AttributeSet attrs)");
        initView(context);
    }

    private void initView(Context context) {

        this.context = context;

        mainHandler = new Handler(context.getMainLooper());

        initWffr();

        if (!CameraController.hasCamera(context)) {
            Log.i(TAG, "initView: No Camera Support");
            Toast.makeText(context, "No Camera Support", Toast.LENGTH_SHORT).show();
            return;
        }
        numberOfCameras = CameraController.getNumberOfCameras(context);

        Log.i(TAG, "initView: getNumberOfCameras " + numberOfCameras);



    }

    private void initWffr() {

    }

    private void exitWffr() {
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.i(TAG, "onDraw: ");
        Log.e("=============", canvas.getWidth() + ", " + canvas.getHeight());
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Log.i(TAG, "onAttachedToWindow: ");

        start();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Log.i(TAG, "onDetachedFromWindow: ");

        stop();
        exitWffr();
    }

    private void start() {
        cameraDetectView = new CameraDetectView(context);
        if (useCamera0) {
            cameraController = new CameraController(context, false, "", "",cameraDetectView);
            cameraController.setListener(this.cameraCallback);
        }
        if (useCamera0) {
            cameraController.openFrontFacingCameraGingerbread();
            previewSurface = new CameraPreview(context, cameraController);
            addView(previewSurface);
        }

        addView(cameraDetectView);
        if (useCamera0)
            bringChildToFront(previewSurface);
        bringChildToFront(cameraDetectView);

    }

    private void stop() {
        try {
            removeAllViews();
            if (useCamera0) {
                previewSurface.getHolder().removeCallback(previewSurface);
                cameraController.releaseCamera();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    CameraController.CameraControllerListener cameraCallback = new CameraController.CameraControllerListener() {

        @Override
        public void onDraw(final int[] faceArray,final int color) {
            mainHandler.post(new Runnable() {
                @Override
                public void run() {
            cameraDetectView.setVisibility(View.VISIBLE);
            cameraDetectView.setRectAndColor(faceArray[0], faceArray[1], faceArray[2], faceArray[3],color);
            cameraDetectView.invalidate();
                }

            });
        }

        @Override
        public void onError(int error) {
            Log.e(TAG, "onError: " + error);
            mainHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    stop();
                    start();
                }
            }, 500);
        }


    };




}
